package com.example.hongyu.avtt.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.hongyu.avtt.R;
import com.example.hongyu.avtt.event.Event;
import com.example.hongyu.avtt.listener.OnItemClickListener;
import com.example.hongyu.avtt.model.Vedio;
import com.example.hongyu.avtt.model.entity.BaseResponse;
import com.example.hongyu.avtt.network.BaseObserver;
import com.example.hongyu.avtt.network.RetrofitHelper;
import com.example.hongyu.avtt.network.RxTransformer;
import com.example.hongyu.avtt.network.api.AppInfoApi;
import com.example.hongyu.avtt.ui.adapter.VedioListAdapter;
import com.example.hongyu.avtt.ui.base.BaseActivity;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ListActivity extends BaseActivity {
    @BindView(R.id.recycle_view)
    XRecyclerView xRecyclerView;

    VedioListAdapter vedioListAdapter;
    List<Vedio> vedios=new ArrayList<>();
    private int pageIndex=1;
    private int pageSize=20;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initActivityData() {
        htppGetHtmlList(pageIndex,true);
    }

    @Override
    protected void initContentView() {
        xRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        vedioListAdapter=new VedioListAdapter(this,vedios);
        xRecyclerView.setAdapter(vedioListAdapter);
        xRecyclerView.setPullRefreshEnabled(false);
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                pageIndex=1;
                htppGetHtmlList(pageIndex, true);
            }

            @Override
            public void onLoadMore() {
                pageIndex++;
                htppGetHtmlList(pageIndex, false);
            }
        });
        vedioListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                mIntent=getIntent();
                if(mIntent==null){
                    mIntent=new Intent();

                }
                Vedio vedio=vedios.get(position);
                String url=vedio.getPath();
                String name=vedio.getTitle();
                mBundle=getBundle();
                if (mBundle==null){
                    mBundle=new Bundle();
                }
                mBundle.putString("PLAYURL",url);
                mBundle.putString("PLAYNAME",name);
                sendIntent(PlayerActivity.class,mBundle);

            }
        });

    }

    @SuppressLint("AutoDispose")
    private void htppGetHtmlList(int pageIndex, boolean isRefresh) {
        RetrofitHelper.create(AppInfoApi.class)
                .vedioList("","","",pageIndex,pageSize)
                .compose(RxTransformer.transform(bindToLifecycle()))
                .subscribe( new BaseObserver<List<Vedio>>(getLoadingDialog()){
                    @Override
                    public void onSuccess(BaseResponse<List<Vedio>> response) {
                        if (response!=null&&response.getData()!=null){
                            if(isRefresh){
                                vedios.clear();
                                xRecyclerView.refreshComplete();
                            }else {
                                xRecyclerView.loadMoreComplete();
                            }
                            vedios.addAll(response.getData());
                            vedioListAdapter.notifyDataSetChanged();

                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if(isRefresh){
                            xRecyclerView.refreshComplete();
                        }else {
                            xRecyclerView.loadMoreComplete();
                        }
                    }
                });
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_list;
    }

    @Override
    protected void onEventProcess(Event event) {

    }

}